package starfire.gui.defaultModelAndRenderer;

import java.io.*;

import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.*;

public class DirectoryComboBoxRenderer
    extends DefaultListCellRenderer {
  FileSystemView fsv = FileSystemView.getFileSystemView();
  IndentIcon ii = null;
  public Component getListCellRendererComponent(JList list, Object value,
                                                int index, boolean isSelected,
                                                boolean cellHasFocus) {

    super.getListCellRendererComponent(list, value, index, isSelected,
                                       cellHasFocus);

    if (value == null) {
      setText("");
      return this;
    }
    File directory = (File) value;
    setText(getName(directory));
    ii = new IndentIcon(directory);
    ii.depth = ( (LocaleListComboModel) list.getModel()).getDepth(index);
    setIcon(ii);

    return this;
  }

  private String getName(File file) {
    return fsv.getSystemDisplayName(file);
  }
}

class IndentIcon
    implements Icon {
  final static int space = 15;
  Icon icon = null;
  int depth = 0;

  FileSystemView fsv = FileSystemView.getFileSystemView();

  public IndentIcon(File f) {
    icon = fsv.getSystemIcon(f);
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    if (c.getComponentOrientation().isLeftToRight()) {
      icon.paintIcon(c, g, x + depth * space, y);
    }
    else {
      icon.paintIcon(c, g, x, y);
    }
  }

  public int getIconWidth() {
    return icon.getIconWidth() + depth * space;
  }

  public int getIconHeight() {
    return icon.getIconHeight();
  }

}
